package com.xhwl.logistics.strategy.impl;

import com.xhwl.common.dto.cent.vehicle.feign.CreateBiVehicleCardAccessRecordHourFeignDTO;
import com.xhwl.common.dto.cent.vehicle.feign.CreateBiVehicleCardPayRecordHourFeignDTO;
import com.xhwl.common.dto.cent.vehicle.feign.CreateBiVehicleLocationAccessRecordHourFeignDTO;
import com.xhwl.logistics.bo.VehicleParkingLotSecondBO;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.constant.VehicleConstant;
import com.xhwl.logistics.converter.mapstruct.VehicleAccessRecordConverter;
import com.xhwl.logistics.converter.mapstruct.VehiclePayRecordConverter;
import com.xhwl.logistics.converter.mapstruct.VehicleStatisticsConverter;
import com.xhwl.logistics.dao.mapper.IVehicleAccessRecordMapper;
import com.xhwl.logistics.dao.mapper.IVehiclePayRecordMapper;
import com.xhwl.logistics.po.VehicleCardAccessRecordStatisticsPO;
import com.xhwl.logistics.po.VehicleCardPayRecordStatisticsPO;
import com.xhwl.logistics.po.VehicleLocationAccessRecordStatisticsPO;
import com.xhwl.logistics.service.cent.IBIVehicleService;
import com.xhwl.logistics.service.impl.VehicleParkingLotServiceImpl;
import com.xhwl.logistics.strategy.IVehicleParkingLotStatisticsHandler;
import com.xhwl.logistics.util.AboutDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Slf4j
public class VehicleParkingLotDefaultStatisticsHandlerImpl implements IVehicleParkingLotStatisticsHandler {

    @Autowired
    protected VehicleParkingLotServiceImpl vehicleParkingLotService;

    @Autowired
    protected IVehicleAccessRecordMapper vehicleAccessRecordMapper;

    @Autowired
    protected IVehiclePayRecordMapper vehiclePayRecordMapper;

    @Autowired
    protected IBIVehicleService biVehicleService;

    @Autowired
    protected VehicleAccessRecordConverter vehicleAccessRecordConverter;

    @Autowired
    protected VehiclePayRecordConverter vehiclePayRecordConverter;

    @Autowired
    protected VehicleStatisticsConverter vehicleStatisticsConverter;

    protected VehicleParkingLotSecondBO queryParkingLot(Integer projectId) {
        return vehicleParkingLotService.queryParkingLot(projectId, null);
    }

    @Override
    public void statisticsCarportRecordToBi(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {
        log.error("车行默认汇总实现，不支持车位数量的汇总");
    }

    @Override
    public void statisticsCardAccessRecordRecordToBi(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {
        Date processDate = new Date();
        Date ingestionDate = AboutDateUtil.dateMinusHour(AboutDateUtil.theSameDayHourOfDate(processDate, VehicleConstant.vehicleTimeZone), 1);
        Integer projectId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId();

        log.info("statistics card access start!,project id: {}", projectId);

        VehicleParkingLotSecondBO vehicleParkingLotSecondBO = queryParkingLot(projectId);
        String parkingLotName = vehicleParkingLotSecondBO.getParkingLotName();
        String parkId = vehicleParkingLotSecondBO.getParkId();

        List<VehicleCardAccessRecordStatisticsPO> cardAccessRecordForIn = vehicleAccessRecordMapper.accessRecordForCountSecond(vehicleAccessRecordConverter.toQueryVehicleAccessRecordForCountBOForEnter(projectId, parkId, ingestionDate));
        List<VehicleCardAccessRecordStatisticsPO> cardAccessRecordForOut = vehicleAccessRecordMapper.accessRecordForCountSecond(vehicleAccessRecordConverter.toQueryVehicleAccessRecordForCountBOForGoOut(projectId, parkId, ingestionDate));
        CreateBiVehicleCardAccessRecordHourFeignDTO createBiVehicleCardAccessRecordHourFeignDTO = vehicleStatisticsConverter.toCreateBiVehicleCardAccessRecordHourFeignDTO(projectId, parkingLotName, processDate, ingestionDate, cardAccessRecordForIn, cardAccessRecordForOut);

        biVehicleService.addDwsCardAccessRecordForHour(createBiVehicleCardAccessRecordHourFeignDTO);

        log.info("statistics card access end!,project id: {}", projectId);
    }

    @Override
    public void statisticsLocationAccessRecordToBi(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {
        Date processDate = new Date();
        Date ingestionDate = AboutDateUtil.dateMinusHour(AboutDateUtil.theSameDayHourOfDate(processDate, VehicleConstant.vehicleTimeZone), 1);
        Integer projectId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId();

        log.info("statistics location access start!,project id: {}", projectId);

        VehicleParkingLotSecondBO vehicleParkingLotSecondBO = queryParkingLot(projectId);
        String parkingLotName = vehicleParkingLotSecondBO.getParkingLotName();
        String parkId = vehicleParkingLotSecondBO.getParkId();

        List<VehicleLocationAccessRecordStatisticsPO> locationForIn = vehicleAccessRecordMapper.locationAccessRecordForCount(vehicleAccessRecordConverter.toQueryVehicleAccessRecordForCountBOForEnter(projectId, parkId, ingestionDate));
        List<VehicleLocationAccessRecordStatisticsPO> locationForOut = vehicleAccessRecordMapper.locationAccessRecordForCount(vehicleAccessRecordConverter.toQueryVehicleAccessRecordForCountBOForGoOut(projectId, parkId, ingestionDate));

        List<CreateBiVehicleLocationAccessRecordHourFeignDTO> createBiVehicleLocationAccessRecordHourFeignDTOList = vehicleStatisticsConverter.toCreateBiVehicleLocationAccessRecordHourFeignDTOList(projectId, parkingLotName, processDate, ingestionDate, locationForIn, locationForOut);

        biVehicleService.addDwsLocationAccessRecordForHours(createBiVehicleLocationAccessRecordHourFeignDTOList);

        log.info("statistics location access end!,project id: {}", projectId);
    }

    @Override
    public void statisticsCardPayRecordToBi(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {
        Date processDate = new Date();
        Date ingestionDate = AboutDateUtil.dateMinusHour(AboutDateUtil.theSameDayHourOfDate(processDate, VehicleConstant.vehicleTimeZone), 1);
        Integer projectId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId();

        log.info("statistics card pay start!,project id: {}", projectId);

        VehicleParkingLotSecondBO vehicleParkingLotSecondBO = queryParkingLot(projectId);
        String parkingLotName = vehicleParkingLotSecondBO.getParkingLotName();
        String parkId = vehicleParkingLotSecondBO.getParkId();

        List<VehicleCardPayRecordStatisticsPO> vehicleCardPayRecordStatisticsPOList = vehiclePayRecordMapper.cardPayRecordForCount(vehiclePayRecordConverter.toQueryVehiclePayRecordForCountBO(projectId, parkId, ingestionDate));

        CreateBiVehicleCardPayRecordHourFeignDTO createBiVehicleCardPayRecordHourFeignDTO = vehicleStatisticsConverter.toCreateBiVehicleCardPayRecordHourFeignDTO(projectId, parkingLotName, processDate, ingestionDate, vehicleCardPayRecordStatisticsPOList);

        biVehicleService.addDwsCardPayRecordForHour(createBiVehicleCardPayRecordHourFeignDTO);

        log.info("statistics card pay end!,project id: {}", projectId);
    }
}
